package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept) {
        log.info("添加部门");
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/depts")
    public Result List(String name, Integer status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询部门,name={},status={},page={},pageSize={}",name,status,page,pageSize);
        //接受亲请求之后分析需求之后发现需要分页,并且result返回值只有两个分别是totol和lows
        // 所以返回值需要修改为PageResult,调用service中的方法来进行业务逻辑的处理,最后封装PageResult
        PageResult<Dept> list = deptService.selectList(name, status, page, pageSize);
        return Result.success(list);
    }

    @GetMapping("/depts/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询部门,id={}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);

    }

    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        log.info("更新部门,dept={}",dept);
        deptService.update(dept);
        return Result.success();
    }

    @DeleteMapping("/depts/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除部门,id={}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/depts/list")
    public Result allList(){
        log.info("查询所有部门");
        List<Dept> list = deptService.allList();
        return Result.success(list);
    }
}
